package org.feidao.chapter64.controller;

import com.alibaba.fastjson.JSONObject;
import org.feidao.chapter64.producer.FanoutService;
import org.feidao.chapter64.producer.DirectService;
import org.feidao.chapter64.producer.HeadersService;
import org.feidao.chapter64.producer.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by linghufeixia on 2022-12-27.
 */
@RestController
@RequestMapping(value = "/rabbitMq")
public class RabbitMqController {

    @Autowired
    private FanoutService fanoutService;

    @Autowired
    private DirectService directService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private HeadersService headersService;

    @RequestMapping(value = "/sendMessageByFanout", method = RequestMethod.GET)
    public String sendMessageByFanout(@RequestParam(value = "msg") String msg) {
        JSONObject jsonObject = new JSONObject();
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        jsonObject.put("time", time);
        jsonObject.put("data", msg);
        fanoutService.sendData(jsonObject.toJSONString());
        return "success";
    }

    @RequestMapping(value = "/sendMessageByDirect", method = RequestMethod.GET)
    public String sendMessageByDirect(@RequestParam(value = "msg") String msg) {
        JSONObject jsonObject = new JSONObject();
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        jsonObject.put("time", time);
        jsonObject.put("data", msg);
        directService.sendData(jsonObject.toJSONString());
        return "success";
    }

    @RequestMapping(value = "/sendMessageByTopic", method = RequestMethod.GET)
    public String sendMessageByTopic(@RequestParam(value = "msg") String msg) {
        JSONObject jsonObject = new JSONObject();
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        jsonObject.put("time", time);
        jsonObject.put("data", msg);
        topicService.sendData(jsonObject.toJSONString());
        return "success";
    }

    @RequestMapping(value = "/sendMessageByHeaders", method = RequestMethod.GET)
    public String sendMessageByHeaders(@RequestParam(value = "msg") String msg) {
        JSONObject jsonObject = new JSONObject();
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        jsonObject.put("time", time);
        jsonObject.put("data", msg);
        headersService.sendData(jsonObject.toJSONString());
        return "success";
    }
}
